package com.enjoyor.soa.traffic.rest.efoms.dubbo.invoke;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.enjoyor.soa.traffic.api.ubms.dto.DeptInfoDto;
import com.enjoyor.soa.traffic.api.ubms.dto.RegionInfoDto;
import com.enjoyor.soa.traffic.api.ubms.service.dubbo.DubboDeptInfoService;
import com.enjoyor.soa.traffic.api.ubms.service.dubbo.DubboDeviceInfoService;
import com.enjoyor.soa.traffic.api.ubms.service.dubbo.DubboPublicDicService;
import com.enjoyor.soa.traffic.api.ubms.service.dubbo.DubboRegionInfoService;
import com.enjoyor.soa.traffic.api.ubms.service.dubbo.DubboTrafficDicService;
import com.enjoyor.soa.traffic.api.ubms.service.dubbo.DubboVehicleDicService;
import com.enjoyor.soa.traffic.api.ubms.service.dubbo.DubboVideoDeviceRltService;
import com.enjoyor.soa.traffic.frame.security.bean.dto.DataAccessRightDto;
import com.enjoyor.soa.traffic.rest.efoms.helper.CheckPojoHelper;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;
import com.enjoyor.soa.traffic.util.pojo.TreeNode;

@Component
public class DubboUbmsServiceInvoke {

    @Reference(timeout = 3000)
    private DubboDeptInfoService deptService;

    @Reference(timeout = 3000)
    private DubboPublicDicService publicService;
    
	@Reference(timeout = 3000)
	private DubboDeviceInfoService deviceInfoService;
	
	@Reference(timeout = 3000)
	private DubboRegionInfoService regionInfoService;
	
	@Reference(timeout = 3000)
	private DubboVideoDeviceRltService videoDeviceRltService;
	
	@Reference(timeout = 3000)
	private DubboTrafficDicService trafficDicService;
	
	@Reference(timeout = 3000)
	private DubboVehicleDicService vehicleDicService;
    
	/**
	 * 部门树Tree
	 */
	public static List<TreeNode> deptInfoTree;
	
	public ResultPojo getSubDeptinfo(String parentId) {
		return deptService.getDeptInfoByParentId(parentId);
	}
	public ResultPojo getChildDeptInfoById(String parentId) {
		return deptService.getChildDeptInfoById(parentId);
	}
	public ResultPojo getDeptInfo(String deptId, String parentId, String deptName, String parentName, String orderSeq) {
		return deptService.getDeptInfo(deptId, parentId, deptName,parentName,orderSeq);
	}
	public ResultPojo getDeptInfoById(String deptId) {
		return deptService.getDeptInfoById(deptId);
	}
	public ResultPojo getDevInfoList(String devId,String devType,String devName,String deptId,String areaId,String orderSeq){
		return deviceInfoService.getDeviceInfo(devId, devType, devName, deptId, areaId, orderSeq);
	}
	/**
	 * 根据用户数据权限获取部门编号列表
	 * @param List<DataAccessRightDto> dataAccessRights
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<String> getSubDeptIds(List<DataAccessRightDto> dataAccessRights){
		List<String> listIds = new ArrayList<String>();
		for(DataAccessRightDto DataAccessRight:dataAccessRights)
		{
			ResultPojo resultPojo = this.getSubDeptinfo(DataAccessRight.getOrgId());
			if(resultPojo.getAppCode().equals("0"))
			{
				List<DeptInfoDto> listInfo = (List<DeptInfoDto>)resultPojo.getResultList();
				for(DeptInfoDto dto: listInfo)
				{
					listIds.add(dto.getDeptId());
				}	
			}
		}
		List<String> newList = new ArrayList<String>(new HashSet<String>(listIds)); //list去重
		return newList;
	}
	
	/**
	 * 根据参数ParentCode取字典信息
	 * @param parentCode
	 * @return
	 */
	public ResultPojo getPublicDicByParentCode(String parentCode)
	{
		ResultPojo resultPojo = publicService.getPublicDicByParentCode(parentCode);
		return resultPojo;
	}
	

	@SuppressWarnings({ "unchecked", "static-access" })
	public List<TreeNode> getDeptTree(String token,String deptId){
		ResultPojo treeDeptInfo = deptService.getTreeDeptInfo(token,deptId);
		List<TreeNode> treeNodeList = (List<TreeNode>)treeDeptInfo.getResultList();
		this.deptInfoTree = treeNodeList;
		List<TreeNode> object = new ArrayList<TreeNode>();
		for (TreeNode treeNode : treeNodeList) {
			String sourceStr = JSON.toJSONString(treeNode, SerializerFeature.DisableCircularReferenceDetect);
			//前台部门名称字段名需要命名为label
			//String replaceAll = sourceStr.replaceAll("text", "label");
			TreeNode parseObject = JSON.parseObject(sourceStr, TreeNode.class);
			object.add(parseObject);
		}
		return  object;
	}
	
	public ResultPojo getRegionInfo() {
		ResultPojo resultPojo = regionInfoService.getRegionInfo();
		return resultPojo;
	}
	
	public ResultPojo getRegionGroupTree(String parentId) {
		ResultPojo resultPojo = regionInfoService.getRegionGroupTree(parentId, null);
		return resultPojo;
	}
	
	@SuppressWarnings("unchecked")
	public ResultPojo getRegionTree(String parentId) {
		ResultPojo result =  new ResultPojo();
		if(null != regionInfoService) {
			ResultPojo resultPojo = regionInfoService.getRegionInfo();
			if(CheckPojoHelper.isValid(resultPojo)) {
				List<RegionInfoDto> regionList = (List<RegionInfoDto>)resultPojo.getResultList();
				List<TreeNode> treeNodeList = new ArrayList<TreeNode>();
				if(null != parentId && !"".equals(parentId)) {
					for(RegionInfoDto regionInfoDto : regionList) {
						if(parentId.equals(regionInfoDto.getParentId())) { 
							TreeNode parentTree = new TreeNode();
							parentTree.setId(regionInfoDto.getRegionId());
							parentTree.setText(regionInfoDto.getRegionName());
							parentTree.setState("open");
							parentTree.setChecked(false);
							parentTree.setIcon(null);
							parentTree.setChildren(getChildrenTree(regionInfoDto.getRegionId(), regionList));
							treeNodeList.add(parentTree);
						}
					}
				}else {
					for(RegionInfoDto regionInfoDto : regionList) {
						if(null == regionInfoDto.getParentId() || "0".equals(regionInfoDto.getParentId()) 
								|| "浙江省".equals(regionInfoDto.getParentId())) {
							TreeNode parentTree = new TreeNode();
							parentTree.setId(regionInfoDto.getRegionId());
							parentTree.setText(regionInfoDto.getRegionName());
							parentTree.setState("open");
							parentTree.setChecked(false);
							parentTree.setIcon(null);
							parentTree.setChildren(getChildrenTree(regionInfoDto.getRegionId(), regionList));
							treeNodeList.add(parentTree);
						}
					}
				}
				result = ResultHelper.getResult(treeNodeList);
			}
		}
		return result;
	}
	
	public List<TreeNode> getChildrenTree(String parentId, List<RegionInfoDto> regionList) {
		List<TreeNode> childrenList = new ArrayList<TreeNode>();
		for(RegionInfoDto regionInfoDto : regionList) {
			if(parentId.equals(regionInfoDto.getParentId())) {
				TreeNode childrenTree = new TreeNode();
				childrenTree.setId(regionInfoDto.getRegionId());
				childrenTree.setText(regionInfoDto.getRegionName());
				childrenTree.setState("close");
				childrenTree.setChecked(false);
				childrenTree.setIcon(null);
				childrenTree.setChildren(getChildrenTree(regionInfoDto.getRegionId(), regionList));
				childrenList.add(childrenTree);
			}
		}
		return childrenList;
	}
	
	/**
	 * 获取设备关联视频点位信息
	 * @param type 设备类型
	 * @param devId 设备编号
	 * @return
	 */
	public ResultPojo getVideoDeviceRltInfo(String type, String deviceId) {
		ResultPojo resultPojo = videoDeviceRltService.getRltInfo(null, null, deviceId, type, null, null);
		return resultPojo;
	}
	
	/**
	 * 根据dicCode获取方向详细信息；
	 */
	public ResultPojo getTrafficInfo(String parentCode){
		ResultPojo resultPojo = trafficDicService.getTrafficDicByParentCode(parentCode);
		return resultPojo;
	}
	
	
	/**
	 * 根据dicCode获取车辆类型详细信息
	 */
	public ResultPojo getVehicleInfo(String parentCode){
		ResultPojo resultPojo = vehicleDicService.getVehicleDicByParentCode(parentCode);
		return resultPojo;
	}
	
}
